package com.kee.server.manager.job;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;

public class Test {
	
	public static String file2String(String filePath) throws IOException{
		InputStreamReader reader = new InputStreamReader(new FileInputStream(
				filePath), "gbk");
		BufferedReader read = new BufferedReader(reader);
		StringBuilder builder = new StringBuilder();
		String str;
		while ((str = read.readLine()) != null) {
			builder.append(str);
			builder.append("\n");
		}
		return builder.toString();
	}
	
	public static Map<String,String> parseAnswer(String html) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		String h = Jsoup.parse(html).body().html().replaceAll("\n", "");
		for (int i = 1; i <= 40; i++) {
			String regx = "\\>(" + i + ")\\.(.*?)(?=(\\>(" + (i+1) + ")\\.))";
			Pattern pattern = Pattern.compile(regx);
			Matcher m = pattern.matcher(h);
			while (m.find()) {
				map.put(m.group(1), m.group(2) + ">");
			}
		}
		System.out.println("解析到答案题目  " + map.size() + " 道");
		return map;
	}
	
	
	public static Map<String,String> parseExam(String html) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		String h = Jsoup.parse(html).body().html().replaceAll("\n", "");
		for (int i = 1; i <= 40; i++) {
			String regx = "\\>(" + i + ")、(.*?)(?=(\\>(" + (i+1) + ")、))";
			Pattern pattern = Pattern.compile(regx);
			Matcher m = pattern.matcher(h);
			while (m.find()) {
				map.put(m.group(1), m.group(2) + ">");
			}
		}
		System.out.println("解析到试题题目  " + map.size() + " 道");
		return map;
	}
	
	
	
	public static void writer(String filePath,String txt){
		File file  = new File(filePath);
		Writer writer = null;
		try {
			writer = new FileWriter(file);
			writer.write(txt);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(writer!=null){
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		try {
			String html = file2String("F:/baiduyundownload/数学1-2.files/数学1-2-1.html");
			String html1 = file2String("F:/baiduyundownload/数学1-2.files/数学1-2.html");
			parseExam(html1);
			parseAnswer(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
